package com.tuanzi.loan.web.bean.family.largeamount;

import com.tuanzi.loan.business.entity.family.LoanLargeAmountNewPartnerInfo;
import com.tuanzi.loan.business.service.family.LoanChannelNdInfoService;
import com.tuanzi.loan.business.service.family.LoanLargeAmountNewPartnerInfoService;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

/**
 * User: liangZongXi
 * Create time: 14:02 2019/8/21
 * Description:
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanLargeAmountNewPartnerInfoBean extends JSFInfoBean<LoanLargeAmountNewPartnerInfo> {
    private static final long serialVersionUID = -559989136676895957L;

    @Autowired
    private LoanLargeAmountNewPartnerInfoService loanLargeAmountNewPartnerInfoService;
    @Autowired
    private LoanChannelNdInfoService loanChannelNdInfoService;

    private List<String> channelNdInfos;
    private String [] selectedChannel;

    @PostConstruct
    private void init() {
        String idStr = getParamFromRequest("id");
        long id = NumberUtils.toLong(idStr);
        if(StringUtils.isNoneBlank(idStr) && 0 != id){
            LoanLargeAmountNewPartnerInfo one = loanLargeAmountNewPartnerInfoService.findOne(id);
            if(null == one){
                forceRedirect("/views/family/WholeSaleManage/largePartnerList.xhtml");
                return;
            }
            this.$ = one;
            initSelectChannels($);
        }else{
            changeStatusToAdd();
            this.$ = new LoanLargeAmountNewPartnerInfo();
        }

        this.channelNdInfos = loanChannelNdInfoService.findDistinctChannelNd();
    }

    private void initSelectChannels(LoanLargeAmountNewPartnerInfo loanLargeAmountNewPartnerInfo) {
        String channelNdInfo = loanLargeAmountNewPartnerInfo.getChannelNdInfo();
        if(StringUtils.isNoneBlank(channelNdInfo)){
            this.selectedChannel = channelNdInfo.split(",");
        }
    }

    public String onReturn(){
        return redirect("/views/family/WholeSaleManage/largePartnerList.xhtml");
    }

    public void save(){
        Long id = $.getId();
        $.setChannelNdInfo(String.join(",", selectedChannel));

        if(null != id){ //update
            this.$ = loanLargeAmountNewPartnerInfoService.saveOrUpdate($);
        }else{
            LoanLargeAmountNewPartnerInfo allByPartnerAccount = loanLargeAmountNewPartnerInfoService.findAllByPartnerAccount($.getPartnerAccount());
            if(null != allByPartnerAccount){
                addErrorMessage("该合作账户已经存在");
                return;
            }
            this.$ = loanLargeAmountNewPartnerInfoService.saveOrUpdate($);
            changeStatusToView();
        }
        addSuccessfullyMessage();
    }
}
